package com.kelvin.paopao.module.login.register.updateInfo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.CityPickerView;
import com.airsaid.pickerviewlibrary.TimePickerView;
import com.airsaid.pickerviewlibrary.listener.OnSimpleCitySelectListener;
import com.kelvin.kelvinlib.utils.CommonUtil;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.login.register.registerStep2.RegisterStep2Activity;
import com.kelvin.paopao.module.login.register.updateAvatar.UpdateAvatarActivity;
import com.kelvin.paopao.widgets.CustomToolbar;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/7.
 */

public class UpdateInfoActivity extends BaseActivity {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.et_nickname)
    EditText etNickname;
    @BindView(R.id.rb_male)
    RadioButton rbMale;
    @BindView(R.id.rb_female)
    RadioButton rbFemale;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.tv_birthday)
    TextView tvBirthday;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_next_step)
    TextView tvNextStep;

    private TimePickerView mTimePickerView;
    private CityPickerView mCityPickerView;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 城市
     */
    private String location;

    /**
     * 生日
     */
    private String birthday;

    /**
     * email
     */
    private String email;

    /**
     * 性别
     */
    private String gender = "1";

    private String phone;
    private String password;
    private String code;
    private String city, province;
    private String wechat_id;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_update_info;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        phone = getIntent().getStringExtra("phone");
        password = getIntent().getStringExtra("password");
        code = getIntent().getStringExtra("code");
        wechat_id = getIntent().getStringExtra("wechat_id");

        customToolbar.showBackIcon(this);
        customToolbar.setTitle("完善资料");

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == rbFemale.getId()) {
                    gender = "0";
                } else if (checkedId == rbMale.getId()) {
                    gender = "1";
                }
            }
        });
        mTimePickerView = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        mCityPickerView = new CityPickerView(this);

        etNickname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String nickname = etNickname.getText().toString().trim();
                String email = etEmail.getText().toString().trim();
                if (!TextUtils.isEmpty(nickname) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(birthday) && !TextUtils.isEmpty(location)) {
                    tvNextStep.setBackgroundResource(R.drawable.shape_login_btn);
                    tvNextStep.setTextColor(ContextCompat.getColor(UpdateInfoActivity.this, android.R.color.white));
                } else {
                    tvNextStep.setBackgroundResource(R.drawable.shape_login2_btn);
                    tvNextStep.setTextColor(ContextCompat.getColor(UpdateInfoActivity.this, R.color.text_color_dark));
                }
            }
        });
        etEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String nickname = etNickname.getText().toString().trim();
                String email = etEmail.getText().toString().trim();
                if (!TextUtils.isEmpty(nickname) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(birthday) && !TextUtils.isEmpty(location)) {
                    tvNextStep.setBackgroundResource(R.drawable.shape_login_btn);
                    tvNextStep.setTextColor(ContextCompat.getColor(UpdateInfoActivity.this, android.R.color.white));
                } else {
                    tvNextStep.setBackgroundResource(R.drawable.shape_login2_btn);
                    tvNextStep.setTextColor(ContextCompat.getColor(UpdateInfoActivity.this, R.color.text_color_dark));
                }
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.ll_birthday, R.id.ll_location, R.id.tv_next_step})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_birthday:
                showDatePickDialog();
                break;
            case R.id.ll_location:
                showHomeTownDialog();
                break;
            case R.id.tv_next_step:
                gotoNext();
                break;
        }
    }

    /**
     * 进入下一个界面
     */
    private void gotoNext() {
        email = etEmail.getText().toString().trim();
        nickname = etNickname.getText().toString().trim();

        if (TextUtils.isEmpty(email) || TextUtils.isEmpty(nickname) || TextUtils.isEmpty(birthday) || TextUtils.isEmpty(location)) {
            ToastUtil.showToast(this, "请仔细填写哦~~");
            return;
        }
        if (!CommonUtil.isEmail(email)) {
            ToastUtil.showToast(this, "e妹儿格式不正确~~");
            return;
        }

        UpdateAvatarActivity.launch(this, gender, nickname, birthday,
                city, email, phone, password, province, code, wechat_id);
    }


    /**
     * 展示地址选择器
     */
    private void showHomeTownDialog() {
        mCityPickerView.setOnCitySelectListener(new OnSimpleCitySelectListener() {
            @Override
            public void onCitySelect(String prov, String city, String area) {
                // 省、市、区 分开获取
                location = prov + " " + city + " " + area;
                tvLocation.setText(location);
                UpdateInfoActivity.this.city = city;
                UpdateInfoActivity.this.province = province;
            }

            @Override
            public void onCitySelect(String str) {
                // 一起获取

            }
        });
        mCityPickerView.show();
    }

    /**
     * 展示日期选择器
     */
    private void showDatePickDialog() {
        mTimePickerView.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                birthday = CommonUtil.parseDateTime(date, "yyyy-MM-dd");
                tvBirthday.setText(birthday);
            }
        });
        mTimePickerView.show();
    }

    public static void launch(Context context, String phone, String password, String code, String wechat_id) {
        Intent intent = new Intent(context, UpdateInfoActivity.class);
        intent.putExtra("phone", phone);
        intent.putExtra("password", password);
        intent.putExtra("code", code);
        intent.putExtra("wechat_id", wechat_id);
        context.startActivity(intent);
    }

}
